package main;

import main.Link.LinkArray;
import main.SQL.FillSQLProjectModel;
import main.SQL.SQLTaskRsrc;
import main.Trees.ProjectNode;
import org.jdesktop.swingx.treetable.AbstractTreeTableModel;

/**
 * Created by s_shmakov on 02.04.14.
 */
public class KSTreeModel extends AbstractTreeTableModel {

    private ProjectNode curProject;
    private LinkArray linkArray;

    public KSTreeModel(ProjectNode curProject, LinkArray linkArray) {
        this.curProject = curProject;
        this.linkArray = linkArray;
    }

    public void fill () {
        this.curProject = new FillSQLProjectModel().newRoot(curProject);
    }

    public void fillSmet() {
//        for (int i = 0; i < curProject.getRoot().)
    }

    public ProjectNode getTask (int id, ProjectNode node)
    {
        ProjectNode stn = null;
        for (ProjectNode sm : node.getChildren() ) {
            if (sm.getId() == id) return sm;
            if (!sm.isTask() && sm.getChildren() != null) stn = getTask(id, sm);
            if (stn != null) return stn;
        }
        return stn;
    }

    @Override
    public String getColumnName( int column ) {
        switch( column ) {
            case 0: return "ID";
            case 1: return "Наименование";
            case 2: return "Ед изм";
            case 3: return "Физ объем";
            case 4: return "Дата начала";
            case 5: return "Дата окончания";
            case 6: return "ФО?";
            case 7: return "ФО";
            case 8: return "ФО";
            case 9: return "MR";
            case 10: return "T";
            case 11: return "MECH";
            case 12: return "%";
            default: return "Unknown";
        }
    }

    @Override
    public int getColumnCount() {
        return 13;
    }

    @Override
    public Object getValueAt(Object node, int column) {
        ProjectNode treeNode = (ProjectNode) node;
        if ( treeNode.isSmString()) {
            switch(column) {
                case 0: return treeNode.getName();
                case 1: return treeNode.getSmrName();
                case 2: return treeNode.getSmrUnit();
                case 3: return treeNode.getSmrSmrVol();
                case 4: return treeNode.getSmrLinkVol();
                case 5: return treeNode.getSmrSmNum();
                case 6: return treeNode.getIsPV();
                case 7: return treeNode.getPV();
                case 8:
                    String unit = treeNode.getSmrUnit();
                    if (treeNode.getSmrUnit() != null && treeNode.getSmrUnit().length() > 3) {
                        String sup = treeNode.getSmrUnit().substring(0,4);
                        if (sup.contains("1000")) {
                            unit = treeNode.getSmrUnit().replace("1000", "");
                        } else if (sup.contains("100")) {
                            unit = treeNode.getSmrUnit().replace("100", "");
                        } else if (sup.contains("10")) {
                            unit = treeNode.getSmrUnit().replace("10", "");
                        }
                    }

                    return unit;
                case 9: return treeNode.getSmrMrVol();
                case 10: return treeNode.getSmrTVol();
                case 11: return treeNode.getSmrMechVol();
                case 12: return treeNode.getTaskCompletePct();
                default: return "Unknown";
            }
        } else {
            switch(column) {
                case 0: return treeNode.getShortName();
                case 1: return treeNode.getName();
                case 2: return treeNode.getUomValue();
                case 3: return treeNode.getPvValue();
                case 4: return treeNode.getStartDate();
                case 5: return treeNode.getFinishDate();
//                case 6: return treeNode.getIsPV();
//                case 7: return treeNode.getPV();
                case 7:
                    for (SQLTaskRsrc cur : treeNode.getRsrc()) {
                        if (cur.getRsrcShortName().contains("PV")) return cur.getPlannedUnits();
                    }
                    return 0.00;
                case 8:
                    String unit = treeNode.getSmrUnit();
                    if (treeNode.getSmrUnit() != null && treeNode.getSmrUnit().length() > 3) {
                        String sup = treeNode.getSmrUnit().substring(0,4);
                        if (sup.contains("1000")) {
                            unit = treeNode.getSmrUnit().replace("1000", "");
                        } else if (sup.contains("100")) {
                            unit = treeNode.getSmrUnit().replace("100", "");
                        } else if (sup.contains("10")) {
                            unit = treeNode.getSmrUnit().replace("10", "");
                        }
                    }

                    return unit;
//                case 9:
//                    for (SQLTaskRsrc cur : treeNode.getRsrc()) {
//                        if (cur.getRsrcShortName().contains("PV")) return cur.getPlannedUnits();
//                    }
//                    return 0.00;

                case 9:
                    for (SQLTaskRsrc cur : treeNode.getRsrc()) {
                        if (cur.getRsrcShortName().equals("MR")) return cur.getPlannedUnits();
                    }
                    return 0.00;
                case 10:
                    for (SQLTaskRsrc cur : treeNode.getRsrc()) {
                        if (cur.getRsrcShortName().equals("T") || cur.getRsrcShortName().contains("КИПА") || cur.getRsrcShortName().contains("ЭС") || cur.getRsrcShortName().contains("АЗО") || cur.getRsrcShortName().contains("ОБС") || cur.getRsrcShortName().contains("МК") || cur.getRsrcShortName().contains("ТХМ")  || cur.getRsrcShortName().contains("МЕХ") || cur.getRsrcShortName().contains("ТИ")) return cur.getPlannedUnits();
                    }
                    return 0.00;
                case 11:
                    for (SQLTaskRsrc cur : treeNode.getRsrc()) {
                        if (cur.getRsrcShortName().equals("MECH")) return cur.getPlannedUnits();
                    }
                    return 0.00;
                case 12: return treeNode.getTaskCompletePct();

                default: return "Unknown";
            }
        }
    }

    @Override
    public Object getChild(Object node, int index) {
        ProjectNode treeNode = (ProjectNode) node;
        return treeNode.getChildAt(index);
    }

    @Override
    public int getChildCount(Object parent) {
        ProjectNode treeNode = (ProjectNode) parent;
        return treeNode.getChildCount();
    }

    @Override
    public int getIndexOfChild(Object parent, Object child) {
        ProjectNode treeNode = (ProjectNode)parent;
        for(int i = 0; i>treeNode.getChildCount(); i++) {
            if( treeNode.getChildAt(i) == child) return i;
        }
        return 0;
    }

    @Override
    public Object getRoot() {
        return curProject;
    }
}
